package com.jingdianjichi.subject.application.controller;


import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.application.converter.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.converter.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryBOConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;


/*
 * 刷题分类controller
 * */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.SubjectCategoryDTO:{}", JSON.toJSONString(subjectCategoryDTO));

            }
//            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类的类型不能为空");
//            Preconditions.checkArgument(StringUtils.isEmpty(subjectCategoryDTO.getCategoryName()), "分类的名称不能为空");
//            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类的父级id不能为空");

            Objects.requireNonNull(subjectCategoryDTO.getCategoryType(), "分类的类型不能为空");
            Objects.requireNonNull(subjectCategoryDTO.getCategoryName(), "分类的名称不能为空");
            Objects.requireNonNull(subjectCategoryDTO.getParentId(), "分类的父级id不能为空");


            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.
                    Instance.SubjectCategoryDTOConverterBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.SubjectCategoryDTO:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /*
     * 查询岗位大类
     * */
    @GetMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryPrimaryCategory.SubjectCategoryDTO:{}", subjectCategoryDTO);
            }

//            Objects.requireNonNull(subjectCategoryDTO.getCategoryType(), "分类的类型不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.
                    Instance.SubjectCategoryDTOConverterBO(subjectCategoryDTO);

            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.
                    Instance.subjectDtoList(boList);

            return Result.ok(subjectCategoryDTOS);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }

    }

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.SubjectCategoryDTO:{}", subjectCategoryDTO);
            }

            Objects.requireNonNull(subjectCategoryDTO.getParentId(), "分类的父级id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.
                    Instance.SubjectCategoryDTOConverterBO(subjectCategoryDTO);
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.
                    Instance.subjectDtoList(boList);
            return Result.ok(subjectCategoryDTOS);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /*
     * 更新分类
     * */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.SubjectCategoryDTO:{}", subjectCategoryDTO);
            }

            Objects.requireNonNull(subjectCategoryDTO.getId(), "分类的id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.
                    Instance.SubjectCategoryDTOConverterBO(subjectCategoryDTO);

            Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.update:{}", e.getMessage(), e);
            return Result.fail("更新失败");
        }
    }

    /*
     * 删除分类  逻辑删除
     * */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.subjectCategoryDTO:{}", subjectCategoryDTO);
            }

            Objects.requireNonNull(subjectCategoryDTO.getId(), "分类的id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.
                    Instance.SubjectCategoryDTOConverterBO(subjectCategoryDTO);
            subjectCategoryBO.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
            Boolean delete = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(delete);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete:{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }

    /*
     * 查询分类及标签
     * */
    @PostMapping("/queryCategoryAndLabel")
    public Result<SubjectCategoryDTO> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryAndLabel.subjectCategoryDTO:{}", subjectCategoryDTO);
            }

            Objects.requireNonNull(subjectCategoryDTO.getId(), "分类的id不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.
                    Instance.SubjectCategoryDTOConverterBO(subjectCategoryDTO);

            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategoryandLabel(subjectCategoryBO);

            List<SubjectCategoryDTO> dtoList = new LinkedList<>();

            boList.forEach(bo -> {
                SubjectCategoryDTO dto = SubjectCategoryDTOConverter.Instance.BOToDTOConverter(bo);
                List<SubjectLabelDTO> labelDTOList = SubjectLabelDTOConverter.
                        Instance.BOconverterDTO(bo.getLabelBOList());
                dto.setLabelDTOList(labelDTOList);
                dtoList.add(dto);
            });

            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryAndLabel.subjectCategoryDTO:{}", subjectCategoryDTO);
            return Result.fail("查询分类及标签失败");
        }
    }

}
